package org.pixelami.twisted.utils
{
	import flash.utils.ByteArray;
	
	public class StreamTools
	{
		public static function prettyPrint(ba:ByteArray):void
		{
			var str:String = "";
			var hex:String = "";
			var ascii:String = "";
			
			for(var i:uint = 0 ; i < ba.length ; i++)
			{
				var n:int = ba[i];
				ascii += n > 0 ? String.fromCharCode(n) + " " : '0 ';
				var h:String = n.toString(16);
				while(h.length <2) h = '0'+h;
				hex += '0x'+h + "  ";
				if(i % 8 == 7) 
				{
					str += hex + "\t" + ascii + "\n";
					hex = '';
					ascii = '';
				}
			}
			while (hex.length < 48) hex += ' ';
			str += hex + "\t" + ascii;
			trace(str);	

		}
		
		public static function hex2ascii(hexstr:String):String
		{
			var asciistr:String = '';
			var i:uint = 0;
			while(i < hexstr.length)
			{
				var h:String = hexstr.slice(i,i+2);
				//trace(h);
				asciistr += String.fromCharCode(parseInt("0x"+h));
				
				i = i+2;
			}
			trace("Utils.hex2ascii = "+asciistr);
			return asciistr
		}
		
		public static function hex2raw(hexstr:String):ByteArray
		{
			var raw:ByteArray = new ByteArray();
			var i:uint = 0;
			while(i < hexstr.length)
			{
				var h:String = hexstr.slice(i,i+2);
				//trace(h);
				raw.writeByte(parseInt("0x"+h));
				
				i = i+2;
			}
			////trace(asciistr);
			return raw;
		}
		
		
		public function StreamTools()
		{
		}

	}
}